package www.member.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.json.simple.JSONObject;

import www.Action;
import www.ActionForward;
import www.utility.FileUploadRequestWrapper;
import www.utility.Utility;

public class MemberCreateFileupload implements Action {
    String filename = "";
    long filesize ;
    static String allFilename = "";   
    static String allFilesize = "";    
    @Override
    public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        String tempDir = Utility.getRealPath(request, "/member/temp"); 
        String upDir = Utility.getRealPath(request, "/member/storage");
        
        System.out.println("upload_proc.jsp loading...");
       
       String param1 = "";        
       FileItem fileItem = null;  
       
        
       FileUploadRequestWrapper requestWrap = new FileUploadRequestWrapper(request, -1, -1, tempDir);

       request = requestWrap;

     
      fileItem = requestWrap.getFileItem("file1");
      long filesize = fileItem.getSize(); 
     
      
          
      if(filesize > 0) {
          
          filename = FileUploadRequestWrapper.saveNewFile(fileItem, upDir);
          System.out.println("filename: " +  filename + "  / filesize: " + filesize);
//          allFilesize = allFilesize + filesize ;
//          allFilename = allFilename + filename ;
      }
      
      JSONObject obj = new JSONObject();

      obj.put("filename", filename);
      obj.put("filesize",filesize);
        
      System.out.print("bbsCreateFileupload: " +  obj);
      request.setAttribute("obj", obj);
     
      ActionForward forward = new ActionForward();
      forward.setRedirect(false);  // 
      forward.setPath("/obj.jsp");
      return forward;     
   
    }

}
